{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6400311",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b48765fa",
   "metadata": {},
   "source": [
    "# 回顾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bf0aed40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "E,B,C,D\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "def convert(input):\n",
    "    prompt = \"\"\"\n",
    "    按以下规则将0-100的数值，转化为A,B,C,D,E 5个等级:\n",
    "    [80,100]之间 为 A\n",
    "    [60,79]之间 为 B\n",
    "    [40,59]之间 为 C\n",
    "    [20,39]之间 为 D\n",
    "    [0,19]之间 为 E\n",
    "    仅输出数值对应的等级\n",
    "    \"\"\"\n",
    "    response = openai.ChatCompletion.create(\n",
    "        engine=deployment, # engine = \"deployment_name\".\n",
    "        temperature = 0,\n",
    "        messages=[\n",
    "            {\"role\":\"system\",\"content\":prompt},\n",
    "            {\"role\": \"user\", \"content\": input}\n",
    "        ]\n",
    "      )\n",
    "    return (response.choices[0].message.content)\n",
    "\n",
    "print(convert(\"100\"))\n",
    "\n",
    "print(convert(\"15,70,50,23\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e3f23acc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "response = openai.ChatCompletion.create(\n",
    "    engine=deployment, # engine = \"deployment_name\".\n",
    "    temperature = 0,\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"如果11 + 11 = 4， 12 + 12 = 6， 那么13 + 13是多少？\",\n",
    "        }\n",
    "    ]\n",
    "  )\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c3a133b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这个问题的关键在于理解这个数学问题并不是按照常规的加法运算规则来解答的，而是有特殊的规则。\n",
      "\n",
      "首先，我们看到11 + 11 = 4，这个等式的左边是两个相同的两位数相加，右边是一个单个的数字。我们可以推测，这个等式的规则可能是将两位数的每一位相加。\n",
      "\n",
      "例如，对于11 + 11，我们可以将其看作是1 + 1 + 1 + 1，结果是4。\n",
      "\n",
      "同样，对于12 + 12，我们可以将其看作是1 + 2 + 1 + 2，结果是6。\n",
      "\n",
      "所以，对于13 + 13，我们可以将其看作是1 + 3 + 1 + 3，结果是8。\n",
      "\n",
      "所以，13 + 13 = 8。\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "response = openai.ChatCompletion.create(\n",
    "    engine=deployment, # engine = \"deployment_name\".\n",
    "    temperature = 0,\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"如果11 + 11 = 4， 12 + 12 = 6， 那么13 + 13是多少？一步步的思考\",\n",
    "        }\n",
    "    ]\n",
    "  )\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cc50f827",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The cafeteria has 9 apples.\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "response = openai.ChatCompletion.create(\n",
    "    engine=deployment, # engine = \"deployment_name\".\n",
    "    temperature = 0,\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"The cafeteria 23 apples. if they used 20 to make lunch and bought 6 more, how many apples do they have?\",\n",
    "        }\n",
    "    ]\n",
    "  )\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5155fbc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "首先，我们需要知道今天北京的天气情况。这包括温度、湿度、风力、是否有雨雪等信息。这些信息可以通过查看天气预报或使用天气应用获取。\n",
      "\n",
      "第二步，根据天气情况来决定穿什么。如果今天是晴天且温度适中，那么可以选择穿T恤和牛仔裤。如果今天是冷天，那么可能需要穿厚一点的衣服，如羽绒服或者毛衣。如果今天有雨，那么需要准备雨衣或者带伞。\n",
      "\n",
      "第三步，考虑今天的活动安排。如果今天需要在户外活动，那么需要根据天气情况选择合适的户外服装。如果今天主要在室内，那么可以穿得轻松一些。\n",
      "\n",
      "第四步，考虑个人的舒适度和喜好。有些人对冷热的承受能力比较强，可能需要的衣物比较少。有些人可能比较怕冷，需要穿得比较多。此外，每个人的穿衣风格和喜好也是需要考虑的因素。\n",
      "\n",
      "最后，根据以上的考虑，做出决定。\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "response = openai.ChatCompletion.create(\n",
    "    engine=deployment, # engine = \"deployment_name\".\n",
    "    temperature = 0,\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"今天北京适合穿什么？一步步的思考\",\n",
    "        }\n",
    "    ]\n",
    "  )\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7833d0c7",
   "metadata": {},
   "source": [
    "# Zero-shot Learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6bad6b9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "tool = \"\"\"\n",
    "1 tool: python_interpreter, description: use it to execute python code\n",
    "2 tool: web_access, description: use it to get realtime info, input is the question or query \n",
    "\"\"\"\n",
    "\n",
    "react_prompt = f\"\"\"\n",
    "Try your best to anwser user's question, and use the following format:\n",
    "\n",
    "Question: the input question you must answer\n",
    "\n",
    "Thought: you should always think about what to do\n",
    "\n",
    "Action: the action to take, should use one of tools in the given tool list:\n",
    "\n",
    "[{tool}]\n",
    "\n",
    "Action Input: the input to the action\n",
    "\n",
    "Here, you should pause the process and return to wait the outside observation. \n",
    "\n",
    "Observation: the result of the action\n",
    "... (this Thought/Action/Action Input/Observation can repeat N times)\n",
    "\n",
    "Thought: I now know the final answer\n",
    "\n",
    "Final Answer: the final answer to the original input question\n",
    "\"\"\"\n",
    "\n",
    "def react_demo(request):\n",
    "    response = openai.ChatCompletion.create(\n",
    "        engine=deployment, # engine = \"deployment_name\".\n",
    "        temperature = 0,\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": react_prompt,\n",
    "            },\n",
    "            {\"role\": \"user\", \"content\": request}\n",
    "        ]\n",
    "      )\n",
    "    print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f68936ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thought: 需要查询今天北京的天气情况，然后根据天气情况推荐穿什么。\n",
      "\n",
      "Action: web_access\n",
      "\n",
      "Action Input: 今天北京天气如何？\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(react_demo(\"今天北京适合穿什么？\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "702d5c62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thought: Given the weather conditions, it's quite hot in Beijing today. Therefore, light and breathable clothing would be appropriate.\n",
      "\n",
      "Final Answer: 今天北京天气很热，建议穿着轻便，透气的衣物。\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# run the web_access tool to get the Beijing's weather today. For example, \n",
    "weather = \"The weather today in Beijing is 33°C, with 0% precipitation, 31% humidity, and a wind speed of 4 mph.\"\n",
    "\n",
    "next_prompt = \"\"\"\n",
    "Question: 今天北京适合穿什么？\n",
    "\n",
    "Thought: I need to check the current weather in Beijing to give a proper clothing suggestion.\n",
    "\n",
    "Action: web_access\n",
    "\n",
    "Action Input: 今天北京天气如何？\n",
    "\n",
    "Observation: \"\"\" + weather\n",
    "\n",
    "print(react_demo(next_prompt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c68f5888",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
